package com.cmsz.walle.deploy.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cmsz.walle.deploy.dto.RestRsp;
import com.cmsz.walle.deploy.entity.Deployment;
import com.cmsz.walle.deploy.service.BasicService;

@RestController
@RequestMapping("/rest")
public class RestAction {

	@Autowired
	BasicService basicService;

	@RequestMapping("/dep/add")
	public RestRsp addDep(HttpServletRequest request,
			HttpServletResponse response) {
		RestRsp rrsp = new RestRsp();
		String name = request.getParameter("name");
		String desc = request.getParameter("description");
		if (StringUtils.isBlank(name) || StringUtils.isBlank(desc)) {
			rrsp.setCode(-1);
			rrsp.setMsg("name or description is blank");
			return rrsp;
		}

		Deployment dep = new Deployment();
		dep.setName(name);
		dep.setDescription(desc);
		basicService.saveDeployment(dep);

		rrsp.setCode(0);
		rrsp.setMsg("success");
		rrsp.setData(dep);
		return rrsp;
	}
	
	@RequestMapping("/dep/update")
	public RestRsp updateDep(HttpServletRequest request,
			HttpServletResponse response)
	{
		RestRsp rrsp = new RestRsp();
		String id = request.getParameter("depId");
		String name = request.getParameter("name");
		String desc = request.getParameter("description");
		if (StringUtils.isBlank(id) || StringUtils.isBlank(name) || StringUtils.isBlank(desc)) {
			rrsp.setCode(-1);
			rrsp.setMsg("id, name or description is blank");
			return rrsp;
		}

		Deployment dep = new Deployment();
		dep.setId(Integer.parseInt(id));
		dep.setName(name);
		dep.setDescription(desc);
		
		basicService.saveDeployment(dep);

		rrsp.setCode(0);
		rrsp.setMsg("success");
		rrsp.setData(dep);
		return rrsp;
		
	}
	
	@RequestMapping("/dep/delete")
	public RestRsp deleteDep(HttpServletRequest request,
			HttpServletResponse response)
	{
		RestRsp rrsp = new RestRsp();
		String id = request.getParameter("depId");
		if (StringUtils.isBlank(id)) {
			rrsp.setCode(-1);
			rrsp.setMsg("id is blank");
			return rrsp;
		}

		Deployment dep = new Deployment();
		dep.setId(Integer.parseInt(id));
		
		basicService.deleteDeployment(Long.parseLong(id));

		rrsp.setCode(0);
		rrsp.setMsg("delete deployment success");
		return rrsp;
	}
	
	public RestRsp addPha(HttpServletRequest request,
			HttpServletResponse response)
	{
		RestRsp rrsp = new RestRsp();
		String id = request.getParameter("phaId");
		String name=request.getParameter("name");
		String desc=request.getParameter("description");
		String sshUserName=request.getParameter("ssh_username");
		String sshPassword=request.getParameter("ssh_password");
		String hosts=request.getParameter("hosts");
		
		
		
		
		return rrsp;
	}
	
	

}
